//
//  VisitorView.swift
//  JKWeiBo
//
//  Created by 王冲 on 2017/5/28.
//  Copyright © 2017年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

// swift如何定义协议, 必须遵守 NSObjectProtocol
protocol VisitorViewDelete: NSObjectProtocol{

    // 登录点击
    func logionWillClick()
    // 注册点击
    func registerWillClick()
}

class VisitorView: UIView {

    // 定义一个对象保存delegate
    weak var delegate: VisitorViewDelete?
    
    /*
     *  设置未登录界面
     *  isHome:  是否是主页
     *  image:   图片的名字
     *  message: 文本的信息
     */
    func setupVistorInfo(isHome:Bool,image:String,message:String) {
        // 如果不是首页，就隐藏转盘
        iconView.isHidden = !isHome
        // 修该中间图标
        homeIcob.image = UIImage(named:image)
        // 修改文本
        messageLabel.text = message
        messageLabel.height = JKwideHighSize().JKtextStringSize(string:message as NSString, size: CGSize(width:224,height:1000), fontSize: 14).height
    
        loginButton.y = messageLabel.frame.maxY + 10
        zhuceBtn.y = messageLabel.frame.maxY + 10
        
        // 如果是首页就执行下面的动画
        if isHome {
            
            startAnimation()
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 1.添加子控件
        addSubview(iconView)
        
        addSubview(homeIcob)
        addSubview(maskBGView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(zhuceBtn)
        // 2.0布局子控件
        // 2.1.设置背景圈圈
        iconView.centerX = JKscreenW/2.0
        iconView.centerY = JKscreenH/2.0
        // 2.2.设置中间的小房子
        homeIcob.centerX = JKscreenW/2.0
        homeIcob.centerY = JKscreenH/2.0
        // 2.3.设置文本
        messageLabel.y = iconView.frame.maxY + 10
        messageLabel.centerX = JKscreenW/2.0
        // 2.4.设置按钮
        zhuceBtn.x = messageLabel.x
        zhuceBtn.y = messageLabel.frame.maxY + 10
        
        loginButton.x = messageLabel.frame.maxX - 100
        loginButton.y = messageLabel.frame.maxY + 10
        
        // 2.5.设置蒙版
        maskBGView.centerX = JKscreenW/2.0
        maskBGView.centerY = JKscreenH/2.0
        
    }
    // swift推荐我们自定义一个控件，要么用纯代码，要么用xib或者stroyboard
    required init?(coder aDecoder: NSCoder) {
        
        // 如果通过xib或者stroyboard创建该类，那么就会崩溃
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: 懒加载一些控件
    // 转盘
    private lazy var iconView: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    
    }()
    
    // 图标
    private lazy var homeIcob: UIImageView = {
    
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return iv
    
    }()
    // 文本
    private lazy var messageLabel: UILabel = {
    
        let label = UILabel()
//        label.height = NSString.JKtextStringHeight(string:"", size: CGSize(width:224,height:1000), fontSize: 14)
        label.width = 224
        label.font = UIFont.systemFont(ofSize: 14)
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        label.textColor = UIColor.JKTextGayColor()
        label.text = ""
        
        return label
    
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
    
        let login = UIButton()
        login.width = 100
        login.height = 30
        login.setTitleColor(UIColor.JKTextGayColor(), for: UIControlState.normal)
        login.setTitle("登录", for: UIControlState.normal)
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        login.addTarget(self, action: #selector(VisitorView.click), for: UIControlEvents.touchUpInside)
        return login
        
    }()
    
    // 注册按钮
    private lazy var zhuceBtn: UIButton = {
    
        let zhuce = UIButton()
        zhuce.width = 100
        zhuce.height = 30
        zhuce.setTitleColor(UIColor.orange, for: UIControlState.normal)
        zhuce.setTitle("注册", for: UIControlState.normal)
        zhuce.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        zhuce.addTarget(self, action: #selector(VisitorView.registor), for: UIControlEvents.touchUpInside)
        return zhuce
        
    }()
    
    // 蒙版的添加
    private lazy var maskBGView: UIImageView = {
        // UIImageView 的初始化
        let maskbgView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
        // UIImageView 的frame
//        maskbgView.frame = CGRect(x:10,y:10,width:100,height:100)
        // UIImageView 的图片填充方式
        maskbgView.contentMode = UIViewContentMode.scaleAspectFill
        return maskbgView
    
    }()
    
    // MARK: 在这里执行动画
    private func startAnimation(){
    
    print("动画")
    // 1.创建动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
    // 2.设置动画属性
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        // 特别添加,该属性默认为YES,代表动画只要执行完毕就移除,false 代表不移除
        anim.isRemovedOnCompletion = false
        
    // 3.将动画添加到图层上
    iconView.layer.add(anim, forKey: nil)
        
    
    }
    
    // MARK: 登录按钮
    func click(){
        
        print("登录")
        
        delegate?.logionWillClick()
    }
    
    // MARK: 注册按钮
    func registor(){
        
        print("注册")
        delegate?.registerWillClick()
        
    }
    
    
}
